package com.shop.user.api.service.impl;

import com.shop.common.entity.user.UserPay;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderPayInput;
import com.shop.common.model.param.user.UserPayLogInput;
import com.shop.common.model.result.user.UserPayLogOutput;
import com.shop.common.model.result.user.UserPayOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.RedisUtil;
import com.shop.common.util.state.DataStatus;
import com.shop.common.util.state.PayState;
import com.shop.user.api.service.IUserPaySvc;
import com.shop.user.atom.atomsvc.IUserPayAtomSvc;
import com.shop.user.feign.trade.ITradeAtomFeignClientSvc;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月15日  15:43
 */
@Service
public class UserPaySvc extends BaseService implements IUserPaySvc {
    @Autowired
    IUserPayAtomSvc userPayAtomSvc;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ITradeAtomFeignClientSvc tradeAtomFeignClientSvc;

    @Override
    public ServiceBean<UserPayOutput> qryUserPayOutput(Integer userId) {
        ServiceBean<UserPayOutput> serviceBean = userPayAtomSvc.selectUserPay(userId);
        if (!serviceBean.isSuccess()) {
            return serviceBean;
        }
        UserPayOutput userPayOutput = serviceBean.getData();
        userPayOutput.clean();
        return serviceBean;
    }

    @Override
    public ServiceBean<List<UserPayLogOutput>> qryUserPayLogs(UserPayLogInput input) {
        return userPayAtomSvc.qryUserPayLogs(input);
    }

    @Override
    public ServiceBean<List<UserPayLogOutput>> qryUserPayLog4P(UserPayLogInput input) {
        return userPayAtomSvc.qryUserPayLog4P(input);
    }

    @Override
    public ServiceBean<Integer> addIntegralRecharge(String payPwd, Integer integral, Integer userId) {
        ServiceBean<String> serviceBean = validatePayPwd(userId,DigestUtils.md5Hex(payPwd));
        if (serviceBean.isFaild()){
            return resultFailed("支付密码错误");
        }
        OrderPayInput orderPayInput = new OrderPayInput();
        orderPayInput.setType(2);
        orderPayInput.setPayPrice(new BigDecimal(integral/10));
        orderPayInput.setBuyerId(userId);
        orderPayInput.setDataStatus(DataStatus.USE.value());
        orderPayInput.setMchtId(1008612);
        orderPayInput.setComment("用户积分充值："+integral);
        orderPayInput.setPayStatus(PayState.WAIT.value());
        orderPayInput.setCreateTime(DateUtil.newNowDate());
        Integer payId = tradeAtomFeignClientSvc.addPayOrder(orderPayInput);
        return resultData(payId);
    }

    @Override
    public ServiceBean<String> validatePayPwd(Integer userId, String pwd) {
        String key = "validate_pay_pwd_"+userId;
        int n = 0;
        boolean exists=redisUtil.exists(key);
        if (exists){
            String num = redisUtil.get(key);
            n = Integer.valueOf(num);
            if (n>=3){
                return resultFailed("错误次数超过三次，账户已锁定五小时");
            }
        }
        UserPay userPay = userPayAtomSvc.selectUserPay(userId).getData();
        if (StringUtils.isBlank(userPay.getPayPwd())){
            return resultFailed("NOT_PWD");
        }
        if (userPay.getPayPwd().equals(pwd)){
            if (exists){
                redisUtil.del(key);
            }
            return resultSuccess();
        }
        n++;
        if (n>=3){
            redisUtil.set(key,n+"",60*60*5);
        }else{
            redisUtil.set(key,n+"",60*60*1);
        }
        return resultFailed("支付密码错误,当前已错误"+n+"次，错误超过3次将锁定账号5个小时！");
    }
}
